package org.cuijq.thinking.in.spring.dependency.injection;

import static org.springframework.context.annotation.AnnotationConfigUtils.AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME;

import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;
import org.cuijq.thinking.in.spring.dependency.injection.annotation.InjectedUser;
import org.cuijq.thinking.in.spring.dependency.injection.annotation.MyAutowaired;
import org.cuijq.thinking.in.spring.ioc.domain.SuperUser;
import org.cuijq.thinking.in.spring.ioc.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;

/**
 * @author by CuiJQ
 * @Classname XmlDepencySetterInjectionDemo
 * @Description XmlDepencySetterInjectionDemo Description
 * @Date 2021/6/29 9:04
 */
public class AutowireAnnotationBeanPostProcessor {

  @MyAutowaired
  private User user;

  @InjectedUser
  private UserHolder injectUserHolder;

  /**
   * 添加新的注解
   * @return
   */
//  @Bean(name = AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME)
//  public static AutowiredAnnotationBeanPostProcessor beanPostProcessor(){
//    AutowiredAnnotationBeanPostProcessor beanPostProcessor = new AutowiredAnnotationBeanPostProcessor();
//    Set<Class<? extends Annotation>> autowiredAnnotationTypes =
//        new LinkedHashSet<>(Arrays.asList(Autowired.class,InjectedUser.class));
//    beanPostProcessor.setAutowiredAnnotationTypes(autowiredAnnotationTypes);
//    return beanPostProcessor;
//  }

  /**
   * 扩展注解
   * @return
   */
  @Bean
  @Order(Ordered.LOWEST_PRECEDENCE-3)
  public static AutowiredAnnotationBeanPostProcessor beanPostProcessor(){
    AutowiredAnnotationBeanPostProcessor beanPostProcessor = new AutowiredAnnotationBeanPostProcessor();
    beanPostProcessor.setAutowiredAnnotationType(InjectedUser.class);
    return beanPostProcessor;
  }


  public static void main(String[] args) {
    //创建BeanFactory 容器
    AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
    //注册
    applicationContext.register(AutowireAnnotationBeanPostProcessor.class);

    XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(applicationContext);
    String xmlResourcePath = "classpath:/META-INF/dependency-lookuo-context.xml";
    beanDefinitionReader.loadBeanDefinitions(xmlResourcePath);

    applicationContext.refresh();

    AutowireAnnotationBeanPostProcessor demo = applicationContext.getBean(AutowireAnnotationBeanPostProcessor.class);

    User userHolder = demo.user;
    System.out.println(userHolder);

    UserHolder injectUserHolder = demo.injectUserHolder;
    System.out.println(injectUserHolder);

    applicationContext.close();
  }

  @Bean
  @Qualifier("user")
  User user() {
    User user = new User();
    user.setId(1L);
    user.setName("xxx");
    return user;
  }

  ;

  @Bean
  @UserGroup()
  User user1() {
    User user = new User();
    user.setId(2L);
    user.setName("xxxxGroup");
    return user;
  }

  ;


  @Bean
  @Qualifier("superUser")
  SuperUser user2() {
    SuperUser user = new SuperUser();
    user.setId(2L);
    user.setName("superUserXxxxGroup");
    return user;
  }

  @Bean
  public UserHolder userHolder(User user) {
    return new UserHolder(user2());
  }
}
